Ext.define('component.estate.selectWindow', {
	extend : 'Ext.window.Window',
	callbackFun: function (record) {
		Ext.Msg.alert('提示', '回调函数未配置！');
	},
	config:{  
		rStatus: '',
		cStatus: '',
		resourcesCode: '',
		resourcesName: '',
		customerId:''
	},
	initComponent : function() {
		var me = this;
		var rStatus = this.rStatus;
		var cStatus = this.cStatus;
		var resourcesCode = this.resourcesCode;
		var resourcesName = this.resourcesName;
		var customerId = this.customerId;
		var gridStore = Ext.create('Ext.data.Store', {
			 fields: ["id","roomStatus","combinedName","roomName","floor", "propertyType", "billingArea", "buildingArea", "innerArea", "sharingArea",
			             "gardenArea", "basementArea", "freeArea","houseTypeId","roomStatusName","setPrice","unitPrice","totalPrice","valuationType",
			             "calculateUnitPrice","discountRate","discountAmount","floatingRange","discountTotalAmount", "projectId","floorType"],
		    pageSize: 10,  //页容量5条数据
		    //是否在服务端排序 （true的话，在客户端就不能排序）
		    remoteSort: false,
		    proxy: {
		        type: 'ajax',
		        url: contextPath+'/estate/projectRoom/componentList',
		        reader: {   //这里的reader为数据存储组织的地方，下面的配置是为json格式的数据，例如：[{"total":50,"rows":[{"a":"3","b":"4"}]}]
		            type: 'json', //返回数据类型为json格式
		            root: 'rows',  //数据
		            totalProperty: 'total' //数据总条数
		        },
		        actionMethods: {read: 'POST'} 
		    },
		    listeners: {
	        	"beforeload": function (store, op, options) {  
		      		var params = {
		                "rStatus": rStatus,
		                "cStatus": cStatus
		            };  
		            Ext.apply(store.proxy.extraParams, params);   
	        	}
	    	}
		});
		var columns = [{
			text : "ID",
			dataIndex : 'id',
			hidden : true
		},{
			text : "房号",
			dataIndex : 'combinedName',
			width:'75%'
		},{
			text : "房产状态",
			dataIndex : 'roomStatusName'
		},{
			text : "计价方式",
			dataIndex : 'valuationType',
            renderer: function(value,metadata,record){  
                if(value=="1"){
                	value="建筑面积 ";
                }else if(value=="2"){
                	value="套内面积 ";
                }else if(value=="3"){
                	value="套(别墅/排屋)";
                }else if(value=="4"){
                	value="个(车位)";
                }   
                return value;  
            }
		}, {
			text : "建筑面积",
			dataIndex : 'buildingArea'
		}, {
			text : "套内面积",
			dataIndex : 'innerArea'
		}, {
			text : "分摊面积",
			dataIndex : 'sharingArea'
		}, {
			text : "花园面积",
			dataIndex : 'gardenArea'
		}, {
			text : "地下室面积",
			dataIndex : 'basementArea'
		}, {
			text : "赠送面积",
			dataIndex : 'freeArea'
		}, {
			text : "单价",
			dataIndex : 'unitPrice'
		}, {
			text : "总价",
			dataIndex : 'totalPrice'
		}];
		var bbar=Ext.create('Ext.PagingToolbar', {
			store : gridStore,
			displayInfo : true
		})
		var grid=Ext.create('Ext.grid.Panel', {
			title:"房源列表",
			region : 'center',
			minHeight:400,
			store : gridStore,
			columns : columns,
			bbar : bbar,
			selModel:  Ext.create('Ext.selection.CheckboxModel', {
				checkOnly:true,
				mode:'single',
				allowDeselect:true
			}),
			tbar:[{text:'确认选择',iconCls:'icon-add',
						handler:function(){
							var record = grid.getSelectionModel().getLastSelected();
		                	if(null==record){
		                		Ext.Msg.alert('提示', '请选择房源！');
		                		return;
		                	}
		                	Ext.Ajax.request({
								method: "POST",
								url: contextPath + "/marketing/businessAbnorma/vaildBuiness",
								params: {
									roomId:record.data.id
								},
								success: function(response, options){
									var body = Ext.decode(response.responseText);
									if(body.res==0){
										Ext.Msg.alert('提示', body.resMsg);
				                		return;
									}else{
										var jsonRecord=Ext.JSON.encode(record.data);
					                	console.log(jsonRecord);
					                	me.callbackFun(jsonRecord);
					                	me.close();
									}
								}
		                	});
		                	
		                	
					    }
			      }
			]
		});
		
		var io = null;
		if(typeof(menu)=="undefined"||menu==null||menu==""){
			io="menu_0003_0001";
		}else{
			io=menu
		}
		var treeStore = Ext.create('Ext.data.TreeStore',{
			  proxy:{
			    type:'ajax',
			    url:contextPath+'/marketing/priceScheme/treeNodeList?resourcesCode='+io+'&resourcesName=resName_pro_list',
			    reader:{
			      type:'json',
			      root:''
			    },
	            extraParams:{
	            	//认筹、可售
	            	roomStatus:"1,3",
	            	"customerId":customerId
	            }
                
			  },
			  root:{
			    text:'房产树',
			    expanded : true
			  }
		});
		
		
		var clickListener = function(node, event) {
			var id = event.data.id;
			var extraParams=gridStore.getProxy().extraParams; 
			extraParams.treeId=id;
			gridStore.load();
		}
		
		/**
		 * 左侧项目树
		 */
		
		var treePanel = Ext.create('Ext.tree.Panel', {
			id : 'estateTree',
			region : 'west',
			split : true,
			title : '房产树',
			width : 220,
			stateId : 'appnav',
			stateful : true,
			margins : '2 0 0 0',
			collapsible : true,
			animCollapse : true,
			xtype : 'treepanel',
			rootVisible : false,
			store : treeStore,
			listeners : {
				itemclick : clickListener
			}
		});
		
		var defaultConfig = {
				frame:true,
				title : '选择房源',
				width : 700,
				height : 500,
				layout : 'border',
				items:[treePanel,grid]
		};
		Ext.apply(this,defaultConfig);
		this.callParent();
	}
});
